<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>广度优先遍历</title>
</head>
<body>
	<script>
	/*
	1）维护一个队列，队列的初始值为树结构根节点组成的列表，重复执行以下步骤，直到队列为空

	2）取出队列中的第一个元素，进行访问相关操作，然后将其后代元素（如果有）全部追加到队列最后

	时间复杂度为O(n)、空间复杂度为O(n)
	 */
	// 广度优先
	function rangeTree(tree, arr = []) {
	let node,
	list = [...tree];
	while ((node = list.shift())) {
		arr.push(node);
		node.children && list.push(...node.children);
	}
		return arr;
	}

	let tree = [
		{
			id: "1",
			title: "节点 1",
			children: [
				{
					id: "1-1",
					title: "节点 1-1"
				},
				{
					id: "1-2",
					title: "节点 1-2"
				}
			]
		},
		{
			id: "2",
			title: "节点 2",
			children: [
				{
					id: "2-1",
					title: "节点 2-1"
				}
			]
		}
	];
	console.log(rangeTree(tree)); //  ['1', '2', '1-1', '1-2', '2-1']
	</script>
</body>
</html>